<?php
/**
 * Creater: 李青林
 * Date Time : 2018/9/11 15:49
 */

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Models\Test;
use App\Http\Models\User;
use App\Http\Models\Goods;
use App\Http\Models\GoodsCoupon;
use App\Http\Models\GoodsShop;
use App\Http\Models\GoodsSales;
use App\Http\Models\GoodsCategory;
use App\Http\Models\TaobaoCategory;
use App\Http\Models\UserAuth;
use Illuminate\Support\Facades\Cache;
use DB;
use Carbon\Carbon;

class IndexController extends Controller
{
    //首页
    public function index(Request $request)
    {
        $whs = array(
            'goods.goodstype' => 1,
            'goods.goodsstate' => 2,
            //'goods.couponstate' => 0
        );

        //判断是否有搜索
        $wherefind = '';
        $keyword = $request->keyword;
        if (!empty($keyword)) {
            //判断是否是搜索商品ID
            if (is_numeric($keyword)) {
                //$whs['goods.goodsid'] = $keyword;
                $wherefind = 'goods.goodsid=' . $keyword;
            } elseif (strpos($keyword, "http") === 0) {
                $goodid = get_id($keyword);
                if (empty($goodid)) {
                    //return json_encode(['code'=>1,'msg'=>'链接地址不正确']);
                } else {
                    //$whs['goods.goodsid'] = $goodid;
                    $wherefind = 'goods.goodsid=' . $goodid;
                }
            } else {
                $wherefind = 'goods.goodstitle like "%' . $keyword . '%"';
            }
        }

        $intypes = null;//定义活动类型数组
        $key = 'fire_';
        //获取是否有聚划算或淘抢购
        $sid = $this->request->sid;
        if (!empty($sid)) {
            if ($sid == 1 or $sid == 2) {
                $whs['goods_shop.activitytype'] = $sid;//添加条件
            } elseif ($sid == 3) {
                $intypes = [1, 2];
            }
        }
        //排序类型
        $oid = $this->request->oid ? $this->request->oid : 0;
        //升降序
        $sort = $request->sort ? $request->sort : 2;
        //默认是综合
        $ob = array(
            'name' => 'goods.id',
            'val' => $sort == 2 ? 'desc' : 'asc'
        );
        //最新
        if ($oid == 1) {
            $ob['name'] = 'goods.created_at';
        } elseif ($oid == 2) {//销量
            $ob['name'] = 'goods_sales.monthsales';
        } elseif ($oid == 3) {//价格
            $ob['name'] = 'goods.voucherprice';
        } elseif ($oid == 4) {//佣金
            $ob['name'] = 'goods.commissionrate';
        } elseif ($oid == 5) {//面额
            $ob['name'] = 'goods_coupon.couponmoney';
        }
        $key = $key . $oid;
        //获取类目ID
        $cid = $this->request->cid;
        //如果有查询类目下面数据
        if (!empty($cid)) {
            $whs['goods.cateid'] = $cid;//添加条件
            $key = $key . $oid . '_' . $cid;
        }
        $page = request('page') ? request('page') : 1;
        $key = $key . '_' . $page;
        //每页显示数据个数
        $limit = env('LIMIT_Index');
        //如果没有选择类目
        Cache::forget($key);
        $list = getList($key, $whs, $limit, $ob, $intypes, $wherefind);
        $keyword = $request->keyword;
        $search = ['oid' => $oid, 'sid' => $sid, 'cid' => $cid, 'keyword' => $keyword, 'sort' => $sort];
        $category = Goods::getCategory();
        return view("home/index", compact('list', 'search', 'category'))->with(['route' => 'index']);
    }

    public function search(Request $request)
    {
        $whs = array(
            'goods.goodsstate' => 2,
            //'goods.couponstate' => 0
        );

        //判断是否有搜索
        $keyword = $request->keyword;
		$wherefind='';
        if (!empty($keyword)) {
        	//判断是否是搜索商品ID
        	if (is_numeric($keyword)) {
        		$whs['goods.goodsid'] = $keyword;
        	} elseif (strpos($keyword, "http") === 0) {
        		$goodid = get_id($keyword);
        		if (empty($goodid)) {
        			return json_encode(['code'=>1,'msg'=>'链接地址不正确']);
        		} else {
        			$whs['goods.goodsid'] = $goodid;
        		}
        	} else {
        		$wherefind = 'goods.goodstitle like "%' . $keyword . '%"';
        	}
        }

        $intypes = null;//定义活动类型数组
        $key = 'fire_';
        //获取是否有聚划算或淘抢购
        $sid = $this->request->sid;
        if (!empty($sid)) {
        	if ($sid == 1 or $sid == 2) {
        		$whs['goods_shop.activitytype'] = $sid;//添加条件
        	} elseif ($sid == 3) {
        		$intypes = [1, 2];
        	}
        }
        //排序类型
        $oid = $this->request->oid ? $this->request->oid : 0;
        //升降序
        $sort = $request->sort ? $request->sort : 2;
        //默认是综合
        $ob = array(
        	'name' => 'goods.id',
        	'val' => $sort == 2 ? 'desc' : 'asc'
        );
        //最新
        if ($oid == 1) {
        	$ob['name'] = 'goods.id';
        } elseif ($oid == 2) {//销量
        	$ob['name'] = 'goods_sales.monthsales';
        } elseif ($oid == 3) {//价格
        	$ob['name'] = 'goods.voucherprice';
        } elseif ($oid == 4) {//佣金
        	$ob['name'] = 'goods.commissionrate';
        } elseif ($oid == 5) {//面额
        	$ob['name'] = 'goods_coupon.couponmoney';
        }
        $key = $key . $oid;
        //获取类目ID
        $cid = $this->request->cid;
        //如果有查询类目下面数据
        if (!empty($cid)) {
            $whs['goods.cateid'] = $cid;//添加条件
            $key = $key . $oid . '_' . $cid;
        }
        $page = request('page') ? request('page') : 1;
        $key = $key . '_' . $page;
        //每页显示数据个数
        $limit = env('LIMIT_Index');
        //如果没有选择类目
        Cache::forget($key);
		
        $list = getList($key, $whs, $limit, $ob, $intypes, $wherefind);
        $keyword = $request->keyword;
        $search = ['oid' => $oid, 'sid' => $sid, 'cid' => $cid, 'keyword' => $keyword, 'sort' => $sort];
        $category = Goods::getCategory();
        return view("home/search", compact('list', 'search', 'category'))->with(['route' => 'search']);
    }

    //秒杀平台
    public function seckill(Request $request)
    {
        $whs = array(
            'goods.goodstype' => 2,
            'goods.goodsstate' => 2,
            //'goods.couponstate' => 0
        );
        $rawstr = 'date_add(str_to_date(CONCAT(goods_kill.startdate," ",goods_kill.startfield,":00"),"%Y-%m-%d %H:%i:%s"), interval 1 day)>now()';
        //判断是否有搜索
        $wherefind = '';
        $keyword = $request->keyword;
        if (!empty($keyword)) {
            //判断是否是搜索商品ID
            if (is_numeric($keyword)) {
                //$whs['goods.goodsid'] = $keyword;
                $wherefind = 'goods.goodsid=' . $keyword;
            } elseif (strpos($keyword, "http") === 0) {
                $goodid = get_id($keyword);
                if (empty($goodid)) {
                    //return json_encode(['code'=>1,'msg'=>'链接地址不正确']);
                } else {
                    //$whs['goods.goodsid'] = $goodid;
                    $wherefind = 'goods.goodsid=' . $goodid;
                }
            } else {
                $wherefind = 'goods.goodstitle like "%' . $keyword . '%"';
            }
            $rawstr = $rawstr . ' and ' . $wherefind;
        }

        //如果没有选择类目
        $list = DB::table('goods')
            ->leftjoin('goods_shop', 'goods.id', '=', 'goods_shop.gid')
            ->leftjoin('goods_sales', 'goods.id', '=', 'goods_sales.gid')
            ->leftjoin('goods_coupon', 'goods.id', '=', 'goods_coupon.gid')
            ->leftjoin('goods_kill', 'goods.id', '=', 'goods_kill.gid')
            ->leftjoin('users', 'goods.userid', '=', 'users.id')
            ->select('users.name as username',
                'goods.id', 'goods.goodstitle', 'goods.goodslink', 'goods.coverimage', 'goods.commissiontype', 'goods.commissionrate', 'goods.copywriting', 'goods.copywritingimage', 'goods.origprice', 'goods.voucherprice', 'goods.created_at', 'goods.goodstype',
                'goods_coupon.couponlink', 'goods_coupon.coupontotal', 'goods_coupon.couponremain', 'goods_coupon.couponmoney', 'goods_coupon.reduceprice',
                'goods_sales.monthsales', 'goods_sales.increment', 'goods_sales.onehoursales', 'goods_sales.twohoursales', 'goods_sales.todaysales',
                'goods_kill.keyword', 'goods_kill.startdate', 'goods_kill.startfield', 'goods_kill.comment',
                'goods_shop.shoptype', 'goods_shop.shopname', 'goods_shop.activitytype','goods.userid')
            ->where($whs)->whereRaw($rawstr)->orderBy('startdate', 'asc')->orderBy('startfield', 'asc')->get();
        $keyword = $request->keyword;
        $search = ['keyword' => $keyword];
        return view("home/seckill", compact('list', 'search'))->with(['route' => 'seckill']);
    }

    //预约商品
    public function mark(Request $request)
    {
        $whs = array(
            'goods.goodsstate' => 2,
            //'goods.couponstate' => 0
        );
        $rawstr = 'goods.begintime>now()';
        //判断是否有搜索
        $wherefind = '';
        $keyword = $request->keyword;
        if (!empty($keyword)) {
            //判断是否是搜索商品ID
            if (is_numeric($keyword)) {
                //$whs['goods.goodsid'] = $keyword;
                $wherefind = 'goods.goodsid=' . $keyword;
            } elseif (strpos($keyword, "http") === 0) {
                $goodid = get_id($keyword);
                if (empty($goodid)) {
                    //return json_encode(['code'=>1,'msg'=>'链接地址不正确']);
                } else {
                    //$whs['goods.goodsid'] = $goodid;
                    $wherefind = 'goods.goodsid=' . $goodid;
                }
            } else {
                $wherefind = 'goods.goodstitle like "%' . $keyword . '%"';
            }
            $rawstr = $rawstr . ' and ' . $wherefind;
        }
        $intypes = null;//定义活动类型数组
        $key = 'mark_';
        //获取是否有聚划算或淘抢购
        $sid = $this->request->sid;
        if (!empty($sid)) {
            if ($sid == 1 or $sid == 2) {
                $whs['goods_shop.activitytype'] = $sid;//添加条件
            } elseif ($sid == 3) {
                $intypes = [1, 2];
            }
        }
        //排序类型
        $oid = $this->request->oid ? $this->request->oid : 0;
        //升降序
        $sort = $this->request->sort ? $this->request->sort : 2;
        //默认是综合
        $ob = array(
            'name' => 'goods.id',
            'val' => $sort == 2 ? 'desc' : 'asc'
        );
        //最新
        if ($oid == 1) {
            $ob['name'] = 'goods.id';
        } elseif ($oid == 2) {//销量
            $ob['name'] = 'goods_sales.monthsales';
        } elseif ($oid == 3) {//价格
            $ob['name'] = 'goods.voucherprice';
        } elseif ($oid == 4) {//佣金
            $ob['name'] = 'goods.commissionrate';
        } elseif ($oid == 5) {//面额
            $ob['name'] = 'goods_coupon.couponmoney';
        }
        $key = $key . $oid;
        //获取类目ID
        $cid = $this->request->cid;
        //如果有查询类目下面数据
        if (!empty($cid)) {
            $whs['goods.cateid'] = $cid;//添加条件
            $key = $key . $oid . '_' . $cid;
        }
        $page = request('page') ? request('page') : 1;
        $key = $key . '_' . $page;
        //每页显示数据个数
        $limit = env('LIMIT_Index');
        //如果没有选择类目
        Cache::forget($key);
        $list = null;
        //如果没有选择类目
        if (empty($intypes)) {
            $list = DB::table('goods')
                ->leftjoin('goods_shop', 'goods.id', '=', 'goods_shop.gid')
                ->leftjoin('goods_sales', 'goods.id', '=', 'goods_sales.gid')
                ->leftjoin('goods_coupon', 'goods.id', '=', 'goods_coupon.gid')
                ->leftjoin('users', 'goods.userid', '=', 'users.id')
                ->select('users.name as username',
                    'goods.id', 'goods.goodslink', 'goods.goodstitle', 'goods.coverimage', 'goods.commissiontype', 'goods.commissionrate', 'goods.copywriting', 'goods.copywritingimage', 'goods.origprice', 'goods.voucherprice', 'goods.created_at', 'goods.begintime', 'goods.goodstype',
                    'goods_coupon.couponlink', 'goods_coupon.coupontotal', 'goods_coupon.couponremain', 'goods_coupon.couponmoney', 'goods_coupon.reduceprice',
                    'goods_sales.monthsales', 'goods_sales.increment', 'goods_sales.onehoursales', 'goods_sales.twohoursales', 'goods_sales.todaysales',
                    'goods_shop.shoptype', 'goods_shop.shopname', 'goods_shop.activitytype','goods.userid')
                ->where($whs)->whereRaw($rawstr)->orderBy($ob['name'], $ob['val'])->paginate($limit);
        } else {
            $list = DB::table('goods')
                ->leftjoin('goods_shop', 'goods.id', '=', 'goods_shop.gid')
                ->leftjoin('goods_sales', 'goods.id', '=', 'goods_sales.gid')
                ->leftjoin('goods_coupon', 'goods.id', '=', 'goods_coupon.gid')
                ->leftjoin('users', 'goods.userid', '=', 'users.id')
                ->select('users.name as username',
                    'goods.id', 'goods.goodslink', 'goods.goodstitle', 'goods.coverimage', 'goods.commissiontype', 'goods.commissionrate', 'goods.copywriting', 'goods.copywritingimage', 'goods.origprice', 'goods.voucherprice', 'goods.created_at', 'goods.begintime', 'goods.goodstype',
                    'goods_coupon.couponlink', 'goods_coupon.coupontotal', 'goods_coupon.couponremain', 'goods_coupon.couponmoney', 'goods_coupon.reduceprice',
                    'goods_sales.monthsales', 'goods_sales.increment', 'goods_sales.onehoursales', 'goods_sales.twohoursales', 'goods_sales.todaysales',
                    'goods_shop.shoptype', 'goods_shop.shopname', 'goods_shop.activitytype','goods.userid')
                ->where($whs)->whereRaw($rawstr)->whereIn('goods_shop.activitytype', $intypes)->orderBy($ob['name'], $ob['val'])->paginate($limit);
        }
        $keyword = $request->keyword;
        $search = ['oid' => $oid, 'sid' => $sid, 'cid' => $cid, 'keyword' => $keyword, 'sort' => $sort];
        $category = Goods::getCategory();
        return view("home/mark", compact('list', 'search', 'category'))->with(['route' => 'mark']);
    }

    //实时上新
    public function newly(Request $request)
    {
        $whs = array(
            'goods.goodstype' => 0,
            'goods.goodsstate' => 2,
            //'goods.couponstate' => 0
        );
        //判断是否有搜索
        $wherefind = '';
        $keyword = $request->keyword;
        if (!empty($keyword)) {
            //判断是否是搜索商品ID
            if (is_numeric($keyword)) {
                //$whs['goods.goodsid'] = $keyword;
                $wherefind = 'goods.goodsid=' . $keyword;
            } elseif (strpos($keyword, "http") === 0) {
                $goodid = get_id($keyword);
                if (empty($goodid)) {
                    //return json_encode(['code'=>1,'msg'=>'链接地址不正确']);
                } else {
                    //$whs['goods.goodsid'] = $goodid;
                    $wherefind = 'goods.goodsid=' . $goodid;
                }
            } else {
                $wherefind = 'goods.goodstitle like "%' . $keyword . '%"';
            }
        }
        $intypes = null;//定义活动类型数组
        $key = 'newly_';
        //获取是否有聚划算或淘抢购
        $sid = $this->request->sid;
        if (!empty($sid)) {
            if ($sid == 1 or $sid == 2) {
                $whs['goods_shop.activitytype'] = $sid;//添加条件
            } elseif ($sid == 3) {
                $intypes = [1, 2];
            }
        }
        //排序类型
        $oid = $this->request->oid ? $this->request->oid : 0;
        //升降序
        $sort = $this->request->sort ? $this->request->sort : 2;
        //默认是综合
        $ob = array(
            'name' => 'goods.id',
            'val' => $sort == 2 ? 'desc' : 'asc'
        );
        //最新
        if ($oid == 1) {
            $ob['name'] = 'goods.id';
        } elseif ($oid == 2) {//销量
            $ob['name'] = 'goods_sales.monthsales';
        } elseif ($oid == 3) {//价格
            $ob['name'] = 'goods.voucherprice';
        } elseif ($oid == 4) {//佣金
            $ob['name'] = 'goods.commissionrate';
        } elseif ($oid == 5) {//面额
            $ob['name'] = 'goods_coupon.couponmoney';
        }
        $key = $key . $oid;
        //获取类目ID
        $cid = $this->request->cid;
        //如果有查询类目下面数据
        if (!empty($cid)) {
            $whs['goods.cateid'] = $cid;//添加条件
            $key = $key . $oid . '_' . $cid;
        }
        $page = request('page') ? request('page') : 1;
        $key = $key . '_' . $page;
        //每页显示数据个数
        $limit = env('LIMIT_Index');
        //如果没有选择类目
        Cache::forget($key);
        //DB::enableQueryLog();
        $list = getList($key, $whs, $limit, $ob, $intypes, $wherefind);
        //print_r(DB::getQueryLog());
        $keyword = $request->keyword;
        $search = ['oid' => $oid, 'sid' => $sid, 'cid' => $cid, 'keyword' => $keyword, 'sort' => $sort];
        $category = Goods::getCategory();
        //dd($list->toArray());
        return view("home/newly", compact('list', 'search', 'category'))->with(['route' => 'newly']);
    }

    //销量排行
    public function ranking(Request $request)
    {
        $whs = array(
            'goods.goodstype' => 0,
            'goods.goodsstate' => 2,
            //'goods.couponstate' => 0
        );
		//$wherestr='goods.goodstype=0 and goods.goodsstate=2 and goods.couponstate=0 ';
		$wherestr='goods.goodstype=0 and goods.goodsstate=2  ';
        //判断是否有搜索
        $wherefind = '';
        $keyword = $request->keyword;
        if (!empty($keyword)) {
            //判断是否是搜索商品ID
            if (is_numeric($keyword)) {
                //$whs['goods.goodsid'] = $keyword;
                $wherefind = 'goods.goodsid=' . $keyword;
            } elseif (strpos($keyword, "http") === 0) {
                $goodid = get_id($keyword);
                if (empty($goodid)) {
                    //return json_encode(['code'=>1,'msg'=>'链接地址不正确']);
                } else {
                    //$whs['goods.goodsid'] = $goodid;
                    $wherefind = 'goods.goodsid=' . $goodid;
                }
            } else {
                $wherefind = 'goods.goodstitle like "%' . $keyword . '%"';
            }
        }
        $intypes = null;//定义活动类型数组
        $key = 'ranking_';
        //获取是否有聚划算或淘抢购
        $sid = $this->request->sid;
        if (!empty($sid)) {
            if ($sid == 1 or $sid == 2) {
                $whs['goods_shop.activitytype'] = $sid;//添加条件
				$wherestr=$wherestr.' and goods_shop.activitytype='.$sid;
            } elseif ($sid == 3) {
                $intypes = [1, 2];
				$wherestr=$wherestr.' and goods_shop.activitytype>0';
            }
        }
        //排序类型
        $oid = $this->request->oid ? $this->request->oid : 1;
        //升降序
        $sort = $this->request->sort ? $this->request->sort : 2;
        //默认是综合
        $ob = array(
            'name' => 'goods_sales.onehoursales',
            'val' => $sort == 2 ? 'desc' : 'asc'
        );
        //最新
        if ($oid == 2) {//销量
            $ob['name'] = 'goods_sales.twohoursales';
        } elseif ($oid == 3) {//价格
            $ob['name'] = 'goods_sales.todaysales';
        } elseif ($oid == 4) {//佣金
            $ob['name'] = 'goods.commissionrate';
        }
        $key = $key . $oid;
        //获取类目ID
        $cid = $this->request->cid;
        //如果有查询类目下面数据
        if (!empty($cid)) {
            $whs['goods.cateid'] = $cid;//添加条件
			$wherestr=$wherestr.' and goods.cateid='.$cid;
            $key = $key . $oid . '_' . $cid;
        }
        $page = request('page') ? request('page') : 1;
        $key = $key . '_' . $page;
        //每页显示数据个数
        $limit = env('LIMIT_Index');
        //如果没有选择类目
        Cache::forget($key);
        $list = null;
		
		$wherestr=$wherestr. ' and goods.begintime<now()';
		if (!empty($wherefind)) {
			$wherestr=$wherestr. ' and ' . $wherefind;			
		}
		$goodsQuery= DB::table('goods')
					->leftjoin('goods_shop', 'goods.id', '=', 'goods_shop.gid')
					->leftjoin('goods_sales', 'goods.id', '=', 'goods_sales.gid')
					->leftjoin('goods_coupon', 'goods.id', '=', 'goods_coupon.gid')
					->leftjoin('users', 'goods.userid', '=', 'users.id')
					->select('users.name as username',
							'goods.id', 'goods.goodslink', 'goods.goodstitle', 'goods.coverimage', 'goods.commissiontype', 'goods.commissionrate', 'goods.copywriting', 'goods.copywritingimage', 'goods.origprice', 'goods.voucherprice', 'goods.created_at', 'goods.goodstype',
							'goods_coupon.couponlink', 'goods_coupon.coupontotal', 'goods_coupon.couponremain', 'goods_coupon.couponmoney', 'goods_coupon.reduceprice',
							'goods_sales.monthsales', 'goods_sales.increment', 'goods_sales.onehoursales', 'goods_sales.twohoursales', 'goods_sales.todaysales',
							'goods_shop.shoptype', 'goods_shop.shopname', 'goods_shop.activitytype','goods.userid')
					->whereRaw($wherestr)->orderBy($ob['name'], $ob['val'])->limit(100);//先取100条

		/*$list= Cache::remember($key, 10, function () use ($limit, $goodsQuery) {
			return DB::table(DB::raw("({$goodsQuery->toSql()}) as goodsQuery"))->paginate($limit);//从中再分页
		});*/
        $list=DB::table(DB::raw("({$goodsQuery->toSql()}) as goodsQuery"))->paginate($limit);//从中再分页
        $keyword = $request->keyword;
        $search = ['oid' => $oid, 'sid' => $sid, 'cid' => $cid, 'keyword' => $keyword, 'sort' => $sort];
        $category = Goods::getCategory();
        return view("home/ranking", compact('list', 'search', 'category'))->with(['route' => 'ranking']);
    }
    

    public function productDetail(Request $request)
    {
        $id = $request->id;
        if (empty($id)) {
            return back();
        }
        $data = DB::table('goods')
            ->join('goods_shop', 'goods.id', '=', 'goods_shop.gid')
            ->join('goods_sales', 'goods.id', '=', 'goods_sales.gid')
            ->join('goods_coupon', 'goods.id', '=', 'goods_coupon.gid')
            ->select('goods.id', 'goods.goodsid', 'goods.goodslink', 'goods.goodstype',
                'goods.goodstitle', 'goods.coverimage', 'goods.origprice', 'goods.commissiontype', 'goods.commissionrate', 'goods.copywriting', 'goods.copywritingimage', 'goods.voucherprice', 'goods.begintime', 'goods.goodstype',
                'goods_coupon.couponlink', 'goods_coupon.coupontotal', 'goods_coupon.coupontype', 'goods_coupon.couponmoney',
                'goods_shop.activitytype', 'goods_shop.shopname', 'goods_shop.shoptype','goods.userid')
            ->where('goods.id', $id)->first();
        $search = ['keyword' => ''];
        if (empty($data)) {
            return back();
        }
        $route = $request->route ?: 'index';
        return view("home/product_detail", compact('data', 'search'))->with(['route' => $route]);
    }



    public function seckillDetail(Request $request)
    {
        $id = $this->request->id;//商品ID
        if (empty($id)) {
            return back();
        }
        $whs = array(
            'goods.id' => $id,
        );
        //如果没有选择类目
        $data = DB::table('goods')
            ->leftjoin('goods_shop', 'goods.id', '=', 'goods_shop.gid')
            ->leftjoin('goods_sales', 'goods.id', '=', 'goods_sales.gid')
            ->leftjoin('goods_coupon', 'goods.id', '=', 'goods_coupon.gid')
            ->leftjoin('goods_kill', 'goods.id', '=', 'goods_kill.gid')
            ->leftjoin('users', 'goods.userid', '=', 'users.id')
            ->select('users.name as username', 'users.avatar', 'users.created_at as jointime', 'users.qq', 'goods.id', 'goods.goodslink', 'goods.goodstype',
                'goods.userid', 'goods.goodstitle', 'goods.coverimage', 'goods.commissiontype', 'goods.commissionrate', 'goods.copywriting', 'goods.copywritingimage', 'goods.origprice', 'goods.voucherprice', 'goods.created_at',
                'goods_coupon.couponlink', 'goods_coupon.coupontotal', 'goods_coupon.couponremain', 'goods_coupon.couponmoney', 'goods_coupon.reduceprice',
                'goods_sales.monthsales', 'goods_sales.increment', 'goods_sales.onehoursales', 'goods_sales.twohoursales', 'goods_sales.todaysales',
                'goods_kill.content', 'goods_kill.friends', 'goods_kill.viewcount',
                'goods_shop.shoptype', 'goods_shop.shopname', 'goods_shop.activitytype','goods.userid')
            ->where($whs)->first();
        if (empty($data)) {
            return back();
        }
        $user = DB::table('users as a')
            ->leftjoin('users_auth as e', 'e.user_id', '=', 'a.id')
            ->leftjoin('users_capital as f', 'f.user_id', '=', 'a.id')
            ->select('a.name as username', 'a.phone', 'a.avatar', 'a.signature', 'f.integral', 'f.voucher_number', 'a.qq', 'a.id', 'e.status',
                'a.created_at')->where('a.id', $data->userid)->first();
        $user_time = ceil((time() - strtotime($user->created_at)) / 86400);
        $statistics = ['user_time' => $user_time];
        $search = ['keyword' => ''];
        return view("home/seckill_detail", compact('data', 'search', 'user', 'statistics'))->with(['route' => 'seckill']);
    }

    public function content(Request $request)
    {
        $route = $request->h ? $request->h : 'about';
        return view("home/content")->with(['route' => $route]);
    }

    public function userHome()
    {
        $userId = request('userid');
        if (empty($userId)) {
            return redirect()->route('index');
        }
        $user = DB::table('users as a')
            ->leftjoin('region as b', 'a.city_id', '=', 'b.id')
            ->leftjoin('region as c', 'a.province_id', '=', 'c.id')
            ->leftjoin('region as d', 'a.area_id', '=', 'd.id')
            ->leftjoin('users_auth as e', 'e.user_id', '=', 'a.id')
            ->leftjoin('users_capital as f', 'f.user_id', '=', 'a.id')
            ->select('a.name as username', 'a.phone', 'a.avatar', 'a.signature', 'f.integral', 'f.voucher_number', 'a.qq', 'a.id', 'e.status',
                'b.name as city', 'c.name as province', 'd.name as area', 'a.created_at')->where('a.id', $userId)->first();
        if (empty($user)) {
            return redirect()->route('index');
        }
        $whs = array(
            'goods.userid' => $userId,
            'goods.goodsstate' => 2,
			//'goods.couponstate'=>0
        );
        //排序类型
        $oid = $this->request->oid;
        //升降序
        $sort = $this->request->sort ? $this->request->sort : 2;

        //默认是综合
        $ob = array(
            'name' => 'goods.created_at',
            'val' => $sort == 2 ? 'desc' : 'asc'
        );
        //最新
        if ($oid == 1) {
            $ob['name'] = 'goods.created_at';
        } elseif ($oid == 2) {//销量
            $ob['name'] = 'goods_sales.monthsales';
        } elseif ($oid == 3) {//价格
            $ob['name'] = 'goods.voucherprice';
        } elseif ($oid == 4) {//佣金
            $ob['name'] = 'goods.commissionrate';
        }
        $limit = env('LIMIT_Index');

        $list = DB::table('goods')
            ->leftjoin('goods_shop', 'goods.id', '=', 'goods_shop.gid')
            ->leftjoin('goods_sales', 'goods.id', '=', 'goods_sales.gid')
            ->leftjoin('goods_coupon', 'goods.id', '=', 'goods_coupon.gid')
            ->leftjoin('users', 'goods.userid', '=', 'users.id')
            ->select('users.name as username',
                'goods.id', 'goods.goodstitle', 'goods.goodslink', 'goods.coverimage', 'goods.commissiontype', 'goods.commissionrate', 'goods.copywriting', 'goods.copywritingimage', 'goods.origprice', 'goods.voucherprice', 'goods.created_at', 'goods.begintime', 'goods.goodstype',
                'goods_coupon.couponlink', 'goods_coupon.coupontotal', 'goods_coupon.couponremain', 'goods_coupon.couponmoney', 'goods_coupon.reduceprice',
                'goods_sales.monthsales', 'goods_sales.increment', 'goods_sales.onehoursales', 'goods_sales.twohoursales', 'goods_sales.todaysales',
                'goods_shop.shoptype', 'goods_shop.shopname', 'goods_shop.activitytype','goods.userid')
            ->where($whs)->orderBy($ob['name'], $ob['val'])->paginate($limit);
        $shelfCount = Goods::where($whs)->count();
        $search = ['oid' => $oid,'userid'=>$userId, 'sort' => $sort];
        $ranking = DB::table('users_capital')->where('integral', '>', $user->integral)->count();
        $ranking = $ranking ? $ranking : 1;
        $user_time = ceil((time() - strtotime($user->created_at)) / 86400);
        $statistics = ['all_online_number' => $shelfCount, 'ranking' => $ranking, 'user_time' => $user_time];
        $auth = UserAuth::where('user_id', $userId)->first();
        return view("home/user_home", compact('user', 'list', 'search', 'statistics', 'auth'))->with(['route' => 'userHome']);
    }

    public function openapi(Request $request)
    {
        $route = $request->h ? $request->h : 'detail';
        return view("api/openapi")->with(['route' => $route]);
    }

    public function test()
    {
        return view('home.test');
    }
}